package com.example.chapter05;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class AlertDialogActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_alert;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alert_dialog);
        findViewById(R.id.btn_alert).setOnClickListener(this);
        tv_alert = findViewById(R.id.tv_alert);
    }

    @Override
    public void onClick(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("尊敬的用户");
        builder.setMessage("你真的要卸载我吗？");

        //设置对话框的肯定按钮文本以及监听器
        builder.setPositiveButton("残忍卸载", (dialogInterface, i) -> {
            tv_alert.setText("啊，我被杀了");
            finish();
        });
        //设置对话框的肯否定按钮文本以及监听器
        builder.setNegativeButton("我再想想",(dialogInterface, i) -> {
            tv_alert.setText("谢谢你的不杀之恩");
        });

        //根据建造器构建提醒对话框对象
        AlertDialog dialog = builder.create();
        //显示题诗对话框
        dialog.show();
    }
}